package com.example.likegifmarker.controller.fragments;

import android.content.Context;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import androidx.viewpager.widget.ViewPager;

import com.example.likegifmarker.Model.Beans.ChangeEvent;
import com.example.likegifmarker.R;
import com.example.likegifmarker.view.adapters.ListFragmentAdapter;
import com.example.likegifmarker.view.wdigets.BigSimplePagerTitleView;
import com.kk.utils.ScreenUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/*
 * Created by　Dullyoung on 2020/12/15
 */
public class IndexFragment extends BaseFragment {

    @BindView(R.id.mi_index)
    MagicIndicator mMiIndex;
    @BindView(R.id.vp_index)
    ViewPager mVpIndex;

    public IndexFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void initViews() {
        setIndicator();
    }

    private void setIndicator() {
        List<String> titles = new ArrayList<>();
        titles.add("关注");
        titles.add("发现");
        titles.add("精选");

        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        CommonNavigatorAdapter navigatorAdapter = new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                BigSimplePagerTitleView cornerMarkPagerTitleView = new BigSimplePagerTitleView(context);
                cornerMarkPagerTitleView.setTextSize(18, 18);
                cornerMarkPagerTitleView.setText(titles.get(index));
                cornerMarkPagerTitleView.setPadding(UIUtil.dip2px(context, 10), 0, UIUtil.dip2px(context, 10), 0);
                cornerMarkPagerTitleView.setNormalColor(0xffA6A6A6);
                cornerMarkPagerTitleView.setSelectedColor(getResources().getColor(R.color.white));
                cornerMarkPagerTitleView.setOnClickListener(v -> {
                    mVpIndex.setCurrentItem(index);
                });
                return cornerMarkPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(ScreenUtil.dip2px(getContext(), 10));
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 22));
                indicator.setRoundRadius(UIUtil.dip2px(context, 1));
                indicator.setStartInterpolator(null);
                indicator.setEndInterpolator(null);
                indicator.setColors(getResources().getColor(R.color.white));
                return indicator;
            }
        };
        commonNavigator.setAdapter(navigatorAdapter);
        mMiIndex.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMiIndex, mVpIndex);

        List<BaseFragment> baseFragments = new ArrayList<>();
        baseFragments.add(new IndexDetailFragment(0));
        baseFragments.add(new IndexDetailFragment(1));
        baseFragments.add(new IndexDetailFragment(2));


        ListFragmentAdapter mViewPageAdapter = new ListFragmentAdapter(getChildFragmentManager(), 1, baseFragments);
        mVpIndex.setOffscreenPageLimit(2);
        mVpIndex.setAdapter(mViewPageAdapter);
        mVpIndex.setCurrentItem(2);
        mVpIndex.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != 2) {
                    EventBus.getDefault().post(new ChangeEvent(position));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
